package com.block.datapush.service.apipush;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.common.util.AESUtil;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.DaXiangJinFuUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 大象金服
 */
@Service
public class DaXiangJinFuApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;

    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);


        String queryUrl = pushApiKeyParam.getCheckUrl();

        String mobileMd5 = memberSourceInfo.getMobileMd5().toLowerCase();

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("userPhone", mobileMd5);
        paramMap.put("channelCode", pushApiKeyParam.getApiCode());


        log.info("【大象金服】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【大象金服】撞库结束【result】{}", result);


        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "1")) {
            pushApiResult.setPushSuccess(true);
            return pushApiResult;
        }

        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);
        return pushApiResult;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) throws Exception {





        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);

        String requestUrl = pushApiKeyParam.getRegisterUrl();
        String apiSecret = pushApiKeyParam.getApiSecret();
        String apiKey = pushApiKeyParam.getApiKey();
        String apiCode = pushApiKeyParam.getApiCode();

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("phone", memberSourceInfo.getMobile());
        bizMap.put("id_card", memberSourceInfo.getIdcard());
        bizMap.put("age", memberSourceInfo.getAge());
        bizMap.put("sex", memberSourceInfo.getSex());
        bizMap.put("real_name", memberSourceInfo.getRealname());
        bizMap.put("city", memberSourceInfo.getCity());
        bizMap.put("ip", DaXiangJinFuUtil.getReqIp(memberSourceInfo.getReqIp()));
        bizMap.put("source", apiCode);
        bizMap.put("zhima", DaXiangJinFuUtil.getZhima(memberSourceInfo.getZhima()));//所有数据，都需要换成传600+
        bizMap.put("credit_card", DaXiangJinFuUtil.getXinyongka(memberSourceInfo.getXinyongka()));
        bizMap.put("house", DaXiangJinFuUtil.getHouse(memberSourceInfo.getHouse()));
        bizMap.put("car", DaXiangJinFuUtil.getCar(memberSourceInfo.getCar()));
        bizMap.put("fund", DaXiangJinFuUtil.getFund(memberSourceInfo.getGjj()));
        bizMap.put("social", DaXiangJinFuUtil.getSocial(memberSourceInfo.getShebao()));
        bizMap.put("huabei", DaXiangJinFuUtil.getHuaBei(memberSourceInfo.getHuabei()));
        bizMap.put("baitiao", DaXiangJinFuUtil.getBaiTiao(memberSourceInfo.getBaitiao()));
        bizMap.put("weilidai", 51);
        bizMap.put("amount", DaXiangJinFuUtil.getAmount(memberSourceInfo.getLoanAmount()));
        bizMap.put("career", DaXiangJinFuUtil.getJob(memberSourceInfo.getGongzuo()));
        bizMap.put("licence", 99);
        bizMap.put("educational", DaXiangJinFuUtil.getXueLi(memberSourceInfo.getXueli()));
        bizMap.put("insurance", DaXiangJinFuUtil.getBaoXian(memberSourceInfo.getBaoxian()));
        bizMap.put("equipment", "AndroidOS");

        String jsonString = JSON.toJSONString(bizMap);
        String reqData = AESUtil.encryptBase64(jsonString, apiSecret, apiKey);

        String timestamp = String.valueOf(System.currentTimeMillis());
        String sign = SecureUtil.md5(StrUtil.format("channelCode={}&reqData={}&timestamp={}", apiCode, reqData, timestamp));

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("channelCode",apiCode);
        paramMap.put("reqData",reqData);
        paramMap.put("timestamp",timestamp);
        paramMap.put("sign",sign);


        log.info("【大象金服】导单开始【requestUrl】{}【paramMap】{}【bizMap】{}", requestUrl, JSON.toJSONString(paramMap), JSON.toJSONString(bizMap));
        String result = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(paramMap))
                .execute().body();
        log.info("【大象金服】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer code = resultObject.getInteger("code");
        String msg = resultObject.getString("msg");


        pushApiResult.setCode(code + "");
        pushApiResult.setMessage(msg);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(code), "1")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }
        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());
        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) throws Exception {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        return pushApiResult;
    }

    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();
        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }


    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        return null;
    }






}
